#ifndef WIDGET_H
#define WIDGET_H

#include <QMetaEnum>
#include <QTimer>
#include <QWidget>
#include <vector>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

/**
 * @brief 用QTcpServer和QTcpSocket，实现tcp通信服务端
 */
class Widget : public QWidget {
  Q_OBJECT

public:
  Widget(QWidget *parent = nullptr);
  ~Widget();
  class ClientItem {
  public:
    ClientItem(int s, const QString &i) : socket(s), info(i) {}
    int socket;
    QString info;
  };

  void onDisconnected();
  void onNewConnection(int socket, const QString &ip, quint32 port);
  void checkSocket(ClientItem *item);
private slots:
  void on_btnStart_clicked();
  void onReadyRead();
  void on_btnSend_clicked();

private:
  Ui::Widget *ui;
  int m_tcpServer;
  bool m_start = false;
  QTimer m_timer;

  std::list<ClientItem *> m_clientList; ///< 所有客户端
};
#endif // WIDGET_H
